#!/bin/bash 
# Use this script to install a public key on a virtual machine
# Copyright (C) 2008 Rene Geers (rene.geers@gmail.com) and Bjoern Remseth (la3lma@gmail.com)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

HOST=$1
if [ -z $HOST ]; then
  echo "Exiting: Host not specified"
  exit 1
fi

user=`whoami`
DSA_FILE=~/.ssh/id_dsa.pub
RSA_FILE=~/.ssh/id_rsa.pub
if [ -f $DSA_FILE ]; then
  KEYFILE=$DSA_FILE
elif [ -f $RSA_FILE ]; then
  KEYFILE=$RSA_FILE
else
  echo "Exiting: Public key not found"
  exit 1
fi
PUBKEY=`cat $KEYFILE`

echo "Installing $KEYFILE on $HOST for $user"
ssh $user@$HOST "mkdir -p ~/.ssh;echo \"$PUBKEY\" >> ~/.ssh/authorized_keys;chmod go-w ~/.ssh;chmod go-w ~/.ssh/authorized_keys"

